part of '../widgets.dart';

/// [VanSwitch]属性常量
const kVanSwitchSize = VanCssKey._raw('--van-switch-size');
const kVanSwitchWidth = VanCssKey._raw('--van-switch-width');
const kVanSwitchHeight = VanCssKey._raw('--van-switch-height');
const kVanSwitchNodeSize = VanCssKey._raw('--van-switch-node-size');
const kVanSwitchNodeBackground = VanCssKey._raw('--van-switch-node-background');
const kVanSwitchNodeShadow = VanCssKey._raw('--van-switch-node-shadow');
const kVanSwitchBackground = VanCssKey._raw('--van-switch-background');
const kVanSwitchOnBackground = VanCssKey._raw('--van-switch-on-background');
const kVanSwitchDuration = VanCssKey._raw('--van-switch-duration');
const kVanSwitchDisabledOpacity = VanCssKey._raw('--van-switch-disabled-opacity');


/// [VanSwitch]主题类
@immutable
class VanSwitchTheme extends VanProvider<VanSwitchThemeData> with VanProvide {
  const VanSwitchTheme({super.key, required super.child, required super.provide});

  static VanSwitchThemeData of(BuildContext context) => VanProvider.of<VanSwitchTheme>(context)?.provide ?? VanTheme.of(context).switchTheme;
  static const _prefix = "--van-switch-";
  static bool support(VanCssKey key) => key.key.startsWith(_prefix);

  static dynamic get<T>(BuildContext context, {required VanCssKey key, T? defaultValue}) {
    final VanSwitchThemeData switchTheme = VanSwitchTheme.of(context);
    if (kVanSwitchSize == key) return switchTheme.size.get(context);
    if (kVanSwitchWidth == key) return switchTheme.width.get(context);
    if (kVanSwitchHeight == key) return switchTheme.height.get(context);
    if (kVanSwitchNodeSize == key) return switchTheme.nodeSize.get(context);
    if (kVanSwitchNodeBackground == key) return switchTheme.nodeBackground.get(context);
    if (kVanSwitchNodeShadow == key) return switchTheme.nodeShadow.get(context);
    if (kVanSwitchBackground == key) return switchTheme.background.get(context);
    if (kVanSwitchOnBackground == key) return switchTheme.onBackground.get(context);
    if (kVanSwitchDuration == key) return switchTheme.duration.get(context);
    if (kVanSwitchDisabledOpacity == key) return switchTheme.disabledOpacity.get(context);
    return null;
  }
}

/// [VanSwitch]主题数据类
@immutable
class VanSwitchThemeData with VanProvide {
  final VanCss<double> size;
  final VanCss<double> width;
  final VanCss<double> height;
  final VanCss<double> nodeSize;
  final VanCss<VanBgColor> nodeBackground;
  final VanCss<String> nodeShadow;
  final VanCss<VanBgColor> background;
  final VanCss<VanBgColor> onBackground;
  final VanCss<double> duration;
  final VanCss<double> disabledOpacity;

  /// 构造器
  const VanSwitchThemeData.raw({
    required this.size,
    required this.width,
    required this.height,
    required this.nodeSize,
    required this.nodeBackground,
    required this.nodeShadow,
    required this.background,
    required this.onBackground,
    required this.duration,
    required this.disabledOpacity,
  });

  /// 拷贝
  VanSwitchThemeData copyFrom({
    VanCss<double>? size,
    VanCss<double>? width,
    VanCss<double>? height,
    VanCss<double>? nodeSize,
    VanCss<VanBgColor>? nodeBackground,
    VanCss<String>? nodeShadow,
    VanCss<VanBgColor>? background,
    VanCss<VanBgColor>? onBackground,
    VanCss<double>? duration,
    VanCss<double>? disabledOpacity,
  }) {
    return VanSwitchThemeData.raw(
      size: size ?? this.size,
      width: width ?? this.width,
      height: height ?? this.height,
      nodeSize: nodeSize ?? this.nodeSize,
      nodeBackground: nodeBackground ?? this.nodeBackground,
      nodeShadow: nodeShadow ?? this.nodeShadow,
      background: background ?? this.background,
      onBackground: onBackground ?? this.onBackground,
      duration: duration ?? this.duration,
      disabledOpacity: disabledOpacity ?? this.disabledOpacity,
    );
  }

  /// 反序列化
  static VanSwitchThemeData fromJson(Map<String, dynamic> json) {
    return VanSwitchThemeData.raw(
      size: VanCss(json[kVanSwitchSize.key]),
      width: VanCss(json[kVanSwitchWidth.key]),
      height: VanCss(json[kVanSwitchHeight.key]),
      nodeSize: VanCss(json[kVanSwitchNodeSize.key]),
      nodeBackground: VanCss(json[kVanSwitchNodeBackground.key]),
      nodeShadow: VanCss(json[kVanSwitchNodeShadow.key]),
      background: VanCss(json[kVanSwitchBackground.key]),
      onBackground: VanCss(json[kVanSwitchOnBackground.key]),
      duration: VanCss(json[kVanSwitchDuration.key]),
      disabledOpacity: VanCss(json[kVanSwitchDisabledOpacity.key]),
    );
  }

}

/// [VanSwitch]样式
@immutable
class VanSwitchStyle with VanProvide {
  static of(BuildContext context) => VanProvider.of<VanSwitchStyle>(context);

  final double? size;
  final double? width;
  final double? height;
  final double? nodeSize;
  final VanBgColor? nodeBackground;
  final String? nodeShadow;
  final VanBgColor? background;
  final VanBgColor? onBackground;
  final double? duration;
  final double? opacity;

  VanSwitchStyle({
    this.size,
    this.width,
    this.height,
    this.nodeSize,
    this.nodeBackground,
    this.nodeShadow,
    this.background,
    this.onBackground,
    this.duration,
    this.opacity,
  });

  bool get isNonNull {
    if (size == null) return false;
    if (width == null) return false;
    if (height == null) return false;
    if (nodeSize == null) return false;
    if (nodeBackground == null) return false;
    if (nodeShadow == null) return false;
    if (background == null) return false;
    if (onBackground == null) return false;
    if (duration == null) return false;
    if (opacity == null) return false;
    return true;
  }

  VanSwitchStyle copyFrom({
    double? size,
    double? width,
    double? height,
    double? nodeSize,
    VanBgColor? nodeBackground,
    String? nodeShadow,
    VanBgColor? background,
    VanBgColor? onBackground,
    double? duration,
    double? opacity,
  }) {
    return VanSwitchStyle(
      size: size ?? this.size,
      width: width ?? this.width,
      height: height ?? this.height,
      nodeSize: nodeSize ?? this.nodeSize,
      nodeBackground: nodeBackground ?? this.nodeBackground,
      nodeShadow: nodeShadow ?? this.nodeShadow,
      background: background ?? this.background,
      onBackground: onBackground ?? this.onBackground,
      duration: duration ?? this.duration,
      opacity: opacity ?? this.opacity,
    );
  }
}